﻿using UnityEngine;
using System.Collections;

public class begincolor : MonoBehaviour
{

	// Use this for initialization

	//GameObj
	GameObject cube1;
	GameObject c2;
	GameObject t1;
	int n = 0;


	ArrayList show_cl = new ArrayList ();
	//a.Add (Color.red);
	//a.Remove()

	Color[] s1 = { Color.white, Color.red, Color.green, Color.blue, Color.yellow };
	string[] s2 = { "白", "红", "绿", "蓝", "黄" };
	Color str;
	Color str1;
	string str2;
	bool createc = false;
	int fs = 0;

	void Start ()
	{
		

		cube1 = GameObject.Find ("Cube");
		c2 = GameObject.Find ("Cube2");
		t1 = GameObject.Find ("text");
		InvokeRepeating ("colorbegin", 0, 2);
		InvokeRepeating ("creatgb", 0, 2);

	}
	
	// Update is called once per frame
	void Update ()
	{
		
		//colorbegin ();
		//if (Input.GetKeyDown (KeyCode.Space)) {	
		//
		//}
	}


	void creatgb ()
	{
		if (createc) {
			str1 = s1 [Random.Range (0, 4)];
			GameObject Sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
			Sphere.AddComponent<Rigidbody> ();
			Sphere.gameObject.GetComponent<Transform> ().position = new Vector3 (c2.gameObject.GetComponent<Transform> ().position.x, c2.gameObject.GetComponent<Transform> ().position.y + 10, c2.gameObject.GetComponent<Transform> ().position.z); 
			Sphere.gameObject.GetComponent<MeshRenderer> ().material.color = str1;
		}

	}


	void OnTriggerEnter (Collider other)
	{
		//print (other.gameObject.name);
		//other.gameObject.GetComponents<MeshRenderer> ().material.color;
		this.gameObject.GetComponent<MeshRenderer> ().material.color = other.GetComponent<MeshRenderer> ().material.color;

		if (((Color)show_cl [0]) == other.GetComponent<MeshRenderer> ().material.color && fs < show_cl.Count) {
			//print (show_cl [0]);
			fs++;
			//Color col1;
			//col1 = ((Color)show_cl [0]);
			for (int i = 1; i < show_cl.Count; i++) {				
				show_cl [i - 1] = show_cl [i];
			}
			show_cl [show_cl.Count - 1] = show_cl [0];
			print (show_cl.Count);
			print (fs);
		}

		Destroy (other.gameObject);
	}


	void colorbegin ()
	{
		if (n <= 5) {
			//int m = ;
			str1 = s1 [Random.Range (0, 4)];
			str2 = s2 [Random.Range (0, 4)];
			str = s1 [Random.Range (0, 4)];


			if (str1 == str) {
				colorbegin ();
			} else {
				cube1.gameObject.GetComponent<MeshRenderer> ().material.color = str1;
				t1.gameObject.GetComponent<TextMesh> ().text = str2;
				t1.gameObject.GetComponent<TextMesh> ().color = str;
				n += 1;
				show_cl.Add (str);
			}			  
		} else {
			CancelInvoke ("colorbegin");
			createc = true;  
			Destroy (cube1);
			Destroy (t1);
			for (int i = 0; i < show_cl.Count; i++) {
				print (show_cl [i]);
			}

		}
	}
}
